<?php

namespace App\Repositories;

use App\Models\Item;
use App\Models\ItemContract;
use App\Models\ItemOrder;
use App\Models\Cooperation;
use InfyOm\Generator\Common\BaseRepository as iBaseRepository;
use Illuminate\Support\Facades\DB;

class ItemRepository extends iBaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        'name',
        'logo',
        'regtime',
        'country',
        'state_id',
        'state',
        'state_name',
        'county_id',
        'county',
        'county_name',
        'zip_id',
        'zip_code',
        'zip',
        'zip_name',
        'address',
        'introduction',
        'business',
        'brokerage',
        'web_url',
        'legal_person',
        'salesman_name',
        'salesman_phone',
        'salesman_wechat',
        'salesman_duty',
        'status'
    ];

    /**
     * Configure the Model
     **/
    public function model()
    {
        return Item::class;
    }

    public function findWithoutFail($id, $columns = ['*'])
    {
        try {
            $item = $this->find($id, $columns);
            if ($item)
            {
                $item->logo_path = $item->logo;
                $item->logo = fileUrl($item->logo);
                $item->contracts = $item->hasContracts()->get();
                if ($item->contracts)
                {
                    foreach ($item->contracts as $key => $val)
                    {
                        $item->contracts[$key]->photo_path = $val->photo;
                        $item->contracts[$key]->photo = fileUrl($val->photo);
                    }
                }
            }

            return $item;
        } catch (\Exception $e) {
            return;
        }
    }

    public function create(array $attributes)
    {
        $item = parent::create($attributes);
        if ($item)
        {
            foreach ($attributes['contracts'] as $val)
            {
                $contract = new ItemContract();
                $contract->item_id = $item->id;
                $contract->photo = $val['photo'];
                $contract->save();
            }
        }

        return $item;
    }

    public function update(array $attributes, $id)
    {
        $item = parent::update($attributes, $id);
        if ($item)
        {
            $oldContracts = $item->hasContracts();
            if ($oldContracts)
            {
                $oldContracts->forceDelete();
            }

            foreach ($attributes['contracts'] as $val)
            {
                $contract = new ItemContract();
                $contract->item_id = $item->id;
                $contract->photo = $val['photo'];
                $contract->save();
            }
        }

        return $item;
    }

    /**
     * 获取渠道的项目方列表
     */
    public function getItemsForChannel($channelId, $request)
    {
        return $this->model->select(['items.id', 'items.name', 'items.brokerage', 'items.salesman_name', 'items.salesman_phone',
                'items.country', 'items.state_id', 'items.state', 'items.state_name', 'items.county_id', 'items.county', 'items.county_name', 'items.zip_id', 'items.zip_code', 'items.zip', 'items.zip_name'])
            ->addSelect(DB::raw('IF(cooperations.id > 0, 1, 0) AS is_cooperation'))
            ->leftJoin('cooperations', function ($join) use ($channelId) {
                $join->on('cooperations.item_id', '=', 'items.id')->where('cooperations.channel_id', $channelId)->where('cooperations.status', '<>', Cooperation::STATUS_DEFAULT);
            })
            ->orderBy('items.id', 'desc')
            ->simplePaginate($request['limit'] ?? 20);
    }

    /**
     * 获取渠道的项目方
     */
    public function getItemForChannel($id, $channelId, $request)
    {
        $item = $this->findWithoutFail($id);
        if ($item)
        {
            $item->cooperation = Cooperation::where('channel_id', $channelId)->where('item_id', $id)->first();
        }

        return $item;
    }
}
